package com.cvpoc.hibernate.dao.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.beans.BeanUtils;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.cvpoc.dto.CompetenceDTO;
import com.cvpoc.hibernate.dao.CompetenceDao;
import com.cvpoc.model.Competence;

public class CompetenceDaoImpl extends HibernateDaoSupport implements
		CompetenceDao {

	@Override
	public CompetenceDTO findCompetenceById(int id) {
		Competence competence = (Competence) getHibernateTemplate().get(
				Competence.class, id);

		CompetenceDTO competenceDTO = new CompetenceDTO();

		BeanUtils.copyProperties(competence, competenceDTO);

		return competenceDTO;
	}

	@Override
	public List<CompetenceDTO> findAllCompetence() {
		List<CompetenceDTO> competenceDTOs = new ArrayList<CompetenceDTO>();
		CompetenceDTO target = null;

		List<Competence> competences = getHibernateTemplate().loadAll(Competence.class);

		for (Competence competence2 : competences) {
			target = new CompetenceDTO();
			BeanUtils.copyProperties(competence2, target);
			competenceDTOs.add(target);
		}
		return competenceDTOs;
	}

	@Override
	public int createCompetence(CompetenceDTO competenceDTO) {
		Competence competence = new Competence();

		BeanUtils.copyProperties(competenceDTO, competence);
		int idCreate = (Integer) getHibernateTemplate().save(competence);

		return idCreate;
	}

	@Override
	public void deleteCompetence(int id) {
		Competence competence = (Competence) getHibernateTemplate().load(
				Competence.class, id);
		getHibernateTemplate().delete(competence);
	}

	@Override
	public void updateCompetence(CompetenceDTO competenceDTO) {
		Competence competence = new Competence();

		BeanUtils.copyProperties(competenceDTO, competence);
		getHibernateTemplate().merge(competence);
	}

	public static CompetenceDTO getCompetenceDTO(Competence pCompetence) {
		CompetenceDTO competenceDTO = new CompetenceDTO();
		BeanUtils.copyProperties(pCompetence, competenceDTO);

		return competenceDTO;
	}

	public static Set<CompetenceDTO> getListeCompetenceDTO(
			Collection<Competence> pListeCompetences) {
		Set<CompetenceDTO> listeCompetenceDTO = new HashSet<CompetenceDTO>();
		for (Competence competence : pListeCompetences) {
			listeCompetenceDTO.add(getCompetenceDTO(competence));
		}
		return listeCompetenceDTO;
	}

	public static Competence getCompetence(CompetenceDTO pCompetenceDTO) {
		Competence competence = new Competence();
		BeanUtils.copyProperties(pCompetenceDTO, competence);

		return competence;
	}

	public static Set<Competence> getListeCompetence(
			Collection<CompetenceDTO> pListeCompetencesDTO) {
		Set<Competence> listeCompetence = new HashSet<Competence>();
		for (CompetenceDTO competenceDTO : pListeCompetencesDTO) {
			listeCompetence.add(getCompetence(competenceDTO));
		}
		return listeCompetence;
	}

	@SuppressWarnings("unchecked")
	@Override
	public Set<CompetenceDTO> findCompetenceByIdExperience(int idExperience) {
		return getListeCompetenceDTO(getHibernateTemplate()
				.find("select c from Competence c, ExperienceCompetence ec where c.id=ec.competence.id  and ec.id_experience = "
						+ idExperience));
	}
}
